﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class SysManage_ChoiceRoleTree : System.Web.UI.Page
{
    BaiMaSoft.Conn.DataConn MyDataConn = new BaiMaSoft.Conn.DataConn();
    BaiMaSoft.SysManage.UserManage UMConn = new BaiMaSoft.SysManage.UserManage();
    public SqlConnection myconn;
    public SqlCommand mycomm, mycomm3;
    public SqlDataReader mydr;
    public SqlDataAdapter myadapter, myadapter2;
    public DataSet myds, myds2;
    public DataView mydv, mydv2;
    public DataTable mytb, mytb2;
    public DataRow myrow, myrow2;
    public DataColumn mycolumn, mycolumn2;
    public string filedtotal = "";
    public string filedtotal2 = "";
    public string sDeptHtml = "";
    public string sPersHtml = "";
    protected string sRoleCode = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            try
            {
                hChoiceID .Value  = Request["PageID"].ToString().Trim();
            }
            catch
            {
                hChoiceID.Value = "0";
            }
            if (Session["Baimasoft_Role_Code"].ToString().Trim() == "" || Session["Baimasoft_Role_Code"].ToString().Trim() == null)
            {
                sRoleCode = "";
            }
            else
            {
                sRoleCode = Session["Baimasoft_Role_Code"].ToString().Trim();
            }
            string sIniOrgID = "0";
            IniRoleInfo(sIniOrgID);
        }
    }

    public void IniRoleInfo(string sSOrgID)
    {
        MyDataConn.Connection.Open();
        myadapter = new SqlDataAdapter("SELECT a.Code as ID, a.Name, a.Descr, a.ChildNumber, a.Creator as ParentID, a.Effective,b.Name AS ParentName FROM Sys_Role_Info a LEFT OUTER JOIN Sys_Role_Info b ON b.Code = a.Creator WHERE (a.Effective = '1')", MyDataConn.Connection);
        myds = new DataSet();
        myadapter.Fill(myds, "a");
        mytb = myds.Tables["a"];

        foreach (DataColumn mycolumn in mytb.Columns)
        {
            filedtotal = filedtotal + mycolumn.ColumnName + ",";
        }

        sDeptHtml = sDeptHtml + "<script language=\"javascript\" type=\"text/javascript\">";
        filedtotal = filedtotal.Substring(0, filedtotal.Length - 1);
        sDeptHtml = sDeptHtml + " function tree(" + filedtotal + "){";
        string[] sfiledtotal = filedtotal.Split(',');
        int nfiledtotal = sfiledtotal.Length;
        for (int i = 0; i < nfiledtotal; i++)
        {
            sDeptHtml = sDeptHtml + "this." + sfiledtotal[i] + "=" + sfiledtotal[i] + ";";
        }

        if (mytb.Rows.Count > 0)
        {
            int iRowCount = mytb.Rows.Count - 1;
            sDeptHtml = sDeptHtml + "}var trees =new Array(" + iRowCount + ");";
        }
        else
        {
            sDeptHtml = sDeptHtml + "var trees =new Array(0);";
        }

        for (int j = 0; j < mytb.Rows.Count; j++)
        {
            sDeptHtml = sDeptHtml + "trees[" + j + "] = new tree(";
            string temp = "";
            for (int k = 0; k < mytb.Columns.Count; k++)
            {
                temp = temp + "'" + mytb.Rows[j][k] + "'";
                if (k != mytb.Columns.Count - 1)
                {
                    temp = temp + ",";
                }
            }
            sDeptHtml = sDeptHtml + temp + ");";
        }
        sDeptHtml = sDeptHtml + "show_tree(0,\"" + sRoleCode + "\");";   //system
        sDeptHtml = sDeptHtml + "</script>";
        sDeptHtml = sDeptHtml + "";
        MyDataConn.Connection.Close();
    }
}
